package com.ccnf.web.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.ccnf.core.bean.Response;
import com.ccnf.core.bean.ResponseWithData;
import com.ccnf.core.bean.ResponseWithPage;
import com.ccnf.model.entity.Invitation;
import com.ccnf.model.group.Insert;
import com.ccnf.model.group.Update;
import com.ccnf.service.InvitationService;
import com.ccnf.service.RewardService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(value = "InvitationController", description = "邀请函相关API")
@RestController
@RequestMapping("/invitations")
public class InvitationController {
    @Autowired
    private InvitationService invitationService;
    @Autowired
    private RewardService rewardService;

    @ApiOperation("获取我创建的邀请函列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户ID", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "expoId", value = "展会ID", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页，默认1", defaultValue = "1", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示几条，默认5", defaultValue = "5", paramType = "query")
    })
    @GetMapping("/users/{userId}/personal")
    public ResponseWithPage personal(@PathVariable Long userId,
                                     @RequestParam(required = false) Long expoId,
                                     @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                     @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        page.setSearchCount(false);
        page = invitationService.findPersonalWithPage(expoId, userId, page);
        return ResponseWithPage.success(page.getRecords(), pageNum, pageSize);
    }

    @ApiOperation("获取我发布的邀请函列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户ID", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "expoId", value = "展会ID", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页，默认1", defaultValue = "1", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示几条，默认5", defaultValue = "5", paramType = "query")
    })
    @GetMapping("/users/{userId}/publish")
    public ResponseWithData publish(@PathVariable Long userId,
                                    @RequestParam(required = false) Long expoId,
                                    @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                    @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        page.setSearchCount(false);
        page = invitationService.findPublishByUserId(expoId, userId, page);
        return ResponseWithPage.success(page.getRecords(), pageNum, pageSize);
    }

    @ApiOperation("获取全部可引用的邀请函列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户ID", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "expoId", value = "展会ID", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页，默认1", defaultValue = "1", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示几条，默认5", defaultValue = "5", paramType = "query")
    })
    @GetMapping("/users/{userId}/money")
    public ResponseWithData money(@PathVariable Long userId,
                                  @RequestParam(required = false) Long expoId,
                                  @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        page.setSearchCount(false);
        page = invitationService.findMoneyByUserId(expoId, userId, page);
        return ResponseWithPage.success(page.getRecords(), pageNum, pageSize);
    }

    @ApiOperation("获取我引用的邀请函列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户ID", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "expoId", value = "展会ID", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页，默认1", defaultValue = "1", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示几条，默认5", defaultValue = "5", paramType = "query")
    })
    @GetMapping("/users/{userId}/reference")
    public ResponseWithData reference(@PathVariable Long userId,
                                      @RequestParam(required = false) Long expoId,
                                      @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                      @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        page.setSearchCount(false);
        page = invitationService.findReferenceByUserId(expoId, userId, page);
        return ResponseWithPage.success(page.getRecords(), pageNum, pageSize);
    }

    @ApiOperation("获取被授权的邀请函列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户ID", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "expoId", value = "展会ID", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页，默认1", defaultValue = "1", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示几条，默认5", defaultValue = "5", paramType = "query")
    })
    @GetMapping("/users/{userId}/auth")
    public ResponseWithData auth(@PathVariable Long userId,
                                  @RequestParam(required = false) Long expoId,
                                  @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        page.setSearchCount(false);
        page = invitationService.findAuthByUserId(expoId, userId, page);
        return ResponseWithPage.success(page.getRecords(), pageNum, pageSize);
    }

    @ApiOperation("获取我收藏的邀请函列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户ID", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "expoId", value = "展会ID", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页，默认1", defaultValue = "1", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示几条，默认5", defaultValue = "5", paramType = "query")
    })
    @GetMapping("/users/{userId}/collection")
    public ResponseWithPage collection(@PathVariable Long userId,
                                       @RequestParam(required = false) Long expoId,
                                       @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                       @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        page.setSearchCount(false);
        page = invitationService.findCollectionWithPage(expoId, userId, page);
        return ResponseWithPage.success(page.getRecords(), pageNum, pageSize);
    }

    @ApiOperation("获取回收站的邀请函列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "当前用户ID", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "expoId", value = "展会ID", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页，默认1", defaultValue = "1", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示几条，默认5", defaultValue = "5", paramType = "query")
    })
    @GetMapping("/users/{userId}/trash")
    public ResponseWithPage trash(@PathVariable Long userId,
                                  @RequestParam(required = false) Long expoId,
                                  @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        page.setSearchCount(false);
        page = invitationService.findTrashWithPage(expoId, userId, page);
        return ResponseWithPage.success(page.getRecords(), pageNum, pageSize);
    }

    @ApiOperation("获取首页模板案例邀请函列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "expoId", value = "展会ID", paramType = "query"),
            @ApiImplicitParam(name = "invitationTitle", value = "模糊搜索邀请函标题", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页，默认1", defaultValue = "1", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示几条，默认5", defaultValue = "5", paramType = "query")
    })
    @GetMapping("templates")
    public ResponseWithPage template(@RequestParam(required = false) Long expoId,
                                     @RequestParam(required = false) String invitationTitle,
                                     @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                     @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        page.setSearchCount(false);
        page = invitationService.findTemplateWithPage(page, expoId, invitationTitle);
        return ResponseWithPage.success(page.getRecords(), pageNum, pageSize);
    }

    @ApiOperation("获取首页模板案例邀请函列表")
    @GetMapping("users/{userId}/latest")
    public ResponseWithData latest(@PathVariable Long userId,
                                   @RequestParam(required = false) Long expoId) {
        return ResponseWithData.success(invitationService.findLatestByUserId(expoId, userId));
    }

    /**
     * 录入邀请函信息
     * @param invitation
     */
    @ApiOperation("录入邀请函信息，用于在创建邀请函录入邀请函内容相关和名片信息，会返回邀请函ID")
    @PostMapping
    public Response save(@RequestBody Invitation invitation) {
        return ResponseWithData.success(invitationService.save(invitation));
    }

    /**
     * 更新邀请函信息
     * @param invitation 需要更新的邀请函信息
     */
    @ApiOperation("更新邀请函信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "invitationId", value = "邀请函ID", required = true, dataType = "Long", paramType = "path")
    })
    @PatchMapping("{invitationId}")
    public Response modify(@PathVariable Long invitationId,
                           @RequestBody Invitation invitation) {
        invitation.setInvitationId(invitationId);
        invitationService.modify(invitation);
        return Response.success();
    }

    /**
     * 通过邀请函ID获取邀请函基本信息，注意不带邀请函内容信息
     * @param invitationId 邀请函ID
     */
    @ApiOperation("通过邀请函ID获取邀请函基本信息，注意不带邀请函内容信息")
    @GetMapping("{invitationId}/basic")
    public ResponseWithData basic(@PathVariable("invitationId") Long invitationId) {
        return ResponseWithData.success(invitationService.findBasicById(invitationId));
    }

    /**
     * 通过邀请函ID获取邀请函基本信息
     * @param invitationId 邀请函ID
     */
    @ApiOperation("通过邀请函ID获取邀请函基本信息")
    @GetMapping("{invitationId}/detail")
    public ResponseWithData detail(@PathVariable("invitationId") Long invitationId) {
        return ResponseWithData.success(invitationService.selectById(invitationId));
    }

    /**
     * 通过邀请函ID获取邀请函基本信息，带邀请函内容信息
     * @param invitationId 邀请函ID
     */
    @ApiOperation("通过邀请函ID获取邀请函基本信息，带邀请函内容信息")
    @GetMapping("{invitationId}")
    public ResponseWithData info(@PathVariable("invitationId")
                                   @ApiParam("邀请函ID") Long invitationId,
                                   Long userId) {
        return ResponseWithData.success(invitationService.findDetailByUserIdAndInvitationId(invitationId, userId));
    }

    /**
     * 通过邀请函ID获取创建邀请函的用户信息
     * @param invitationId 邀请函ID
     */
    @ApiOperation("通过邀请函ID获取创建邀请函的用户信息")
    @GetMapping("{invitationId}/user")
    public ResponseWithData createUserInfo(@PathVariable Long invitationId) {
        return ResponseWithData.success(invitationService.findUserInfoByInvitationId(invitationId));
    }

    @ApiOperation("查询用户是否引用过该邀请函数据")
    @GetMapping("isReference")
    public ResponseWithData isReference(Long invitationId, Long userId) {
        return ResponseWithData.success(invitationService.findIsReferenceByInvitationIdAndUserId(invitationId, userId));
    }

    @ApiOperation("获取邀请函的赏金列表")
    @GetMapping("{invitationId}/rewards")
    public ResponseWithData rewards(@PathVariable Long invitationId) {
        return ResponseWithData.success(rewardService.findRewardByInvitationId(invitationId));
    }
}